﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UCLibrary.UC
{
    [DefaultEvent("Click")]
    public partial class UCSwithcBtn : UserControl
    {
        public UCSwithcBtn()
        {
            InitializeComponent();
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);

        }
        private string _strON = "";
        private string _strOFF = "";
        [Category("自定/显示字符")]
        public string strON
        {
            get => _strON;
            set { _strON = value; Refresh(); } 
        }
        [Category("自定/显示字符")]
        public string strOFF
        {
            get => _strOFF;
            set { _strOFF = value; Refresh(); }
        }
        private Font _font = new Font("微软雅黑",15f,FontStyle.Bold);
        [Category("自定/显示字符")]
        public Font font
        {
            get { return _font; }
            set { _font = value; Refresh(); }
        }
        private Color _strColor = Color.White;
        [Category("自定/显示字符")]
        public Color strColor
        {
            get => _strColor;
            set { _strColor = value; Refresh();}
        }
        private bool _isON=false;
        public bool isON
        {
            get { return _isON; }
            set { _isON = value;Refresh(); }
        }
        
        private Color _ONbackgroundColor = Color.Green;
        [Category("自定/背景颜色")]
        public Color ONbackgroundColor
        {
            get { return _ONbackgroundColor; }
            set { _ONbackgroundColor = value; Refresh(); }
        }
        private Color _OFFbackgroundColor = Color.Gray;
        [Category("自定/背景颜色")]
        public Color OFFbackgroundColor
        {
            get { return _OFFbackgroundColor; }
            set { _OFFbackgroundColor = value; Refresh(); }
        }
        
        private Color _ONdotColor = Color.White;
        [Category("自定/小点颜色")]
        public Color ONdotColor
        {
            get { return _ONdotColor; }
            set { _ONdotColor = value; Refresh(); }
        }
        private Color _OFFdotColor = Color.White;
        [Category("自定/小点颜色")]
        public Color OFFdotColor
        {
            get { return _OFFdotColor; }
            set { _OFFdotColor = value; Refresh(); }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            Function.setHighQuality( g);
            if(Width<Height)
                Width = Height;
            int bigDiameter = Height;
            int dotDiameter = Height / 2;
            GraphicsPath regionPath = new GraphicsPath();
            GraphicsPath backgroundPath = new GraphicsPath();
            backgroundPath.AddArc(new Rectangle(0,0,bigDiameter,bigDiameter),90,180);
            backgroundPath.AddLine(bigDiameter/2,0,Width-bigDiameter/2,0);
            backgroundPath.AddArc(new Rectangle(Width-bigDiameter,0,bigDiameter,bigDiameter), -90, 180);
            backgroundPath.AddLine(Width-bigDiameter/2,Height,bigDiameter/2,Height);
            backgroundPath.CloseFigure();

            StringFormat format = new StringFormat();


            if(isON)
            {
                format.Alignment = StringAlignment.Far;
                format.LineAlignment = StringAlignment.Center;
                g.FillPath(new SolidBrush(ONbackgroundColor), backgroundPath);
                g.FillEllipse(new SolidBrush(ONdotColor), Width -3* dotDiameter / 2,(Height - dotDiameter) / 2, dotDiameter, dotDiameter);
                g.DrawString(strON,font, new SolidBrush(strColor), new Rectangle(1, 1,Width/2,Height), format);
            }
            else
            {
                format.Alignment = StringAlignment.Near;
                format.LineAlignment = StringAlignment.Center;
                g.FillPath(new SolidBrush(OFFbackgroundColor), backgroundPath);
                g.FillEllipse(new SolidBrush(OFFdotColor), dotDiameter / 2, (Height - dotDiameter) / 2, dotDiameter, dotDiameter);
                g.DrawString(strOFF, font, new SolidBrush(strColor), new Rectangle(Width / 2, 1, Width / 2, Height), format);
            }
        }

        private void UCSwithcBtn_MouseEnter(object sender, EventArgs e)
        {
            BorderStyle = BorderStyle.FixedSingle;
        }

        private void UCSwithcBtn_MouseLeave(object sender, EventArgs e)
        {
            BorderStyle = BorderStyle.None;
        }
    }
}
